require 'RMagick'
class PagesController < ApplicationController
  before_filter :find_book, :except=>'search'
  before_filter :not_allowed, :only=>[:new, :create, :destroy]
  
  # GET /pages
  # GET /pages.xml
  def index
    @pages = @book.pages

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @pages }
    end
  end

  # GET /pages/1
  # GET /pages/1.xml
  def show
    @page = @book.pages.find_by_page_number(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @page.to_xml }
      format.jpg  { send_file @page.image_file_name, :type=>:jpg, :disposition=>'inline' }
      format.mp3 { send_file @page.sound_file_name, :type=>:mp3, :disposition=>'inline' }
    end
  end

  # GET /pages/new
  # GET /pages/new.xml
  def new
    @page = @book.pages.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @page }
    end
  end

  # GET /pages/1/edit
  def edit
    @page = @book.pages.find_by_page_number(params[:id])
  end

  # POST /pages
  # POST /pages.xml
  def create
    @page = @book.pages.new(params[:page])

    respond_to do |format|
      if @page.save
        flash[:notice] = 'Page was successfully created.'
        format.html { redirect_to(book_page_path(@page.book, @page)) }
        format.xml  { render :xml => @page, :status => :created, :location => @page }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @page.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /books/1/pages/1
  # PUT /books/1/pages/1.xml
  def update
    @page = @book.pages.find_by_page_number(params[:id])
    
    respond_to do |format|
      if params[:sound_file]
        save_sound_file(@page, params[:sound_file])
        
        flash[:notice] = 'Page was successfully updated.'
      end

      format.html { redirect_to(book_page_path(@page.book, @page)) }
      format.xml  { head :ok }
    end
  end

  # DELETE /pages/1
  # DELETE /pages/1.xml
  def destroy
    @page = @book.pages.find_by_page_number(params[:id])
    @page.destroy

    respond_to do |format|
      format.html { redirect_to(book_pages_url(@book)) }
      format.xml  { head :ok }
    end
  end
  
  # GET /books/1/pages/search
  # GET /books/1/pages/search.xml
  def search
    options = {:limit=>20, :offset=>((params[:page] || "1").to_i-1)*20}
    ar_options = {:order=>'page_number'}
    ar_options[:conditions] = ['book_id = ?', params[:book_id]] if params[:book_id]
    @pages = Page.find_by_contents(params[:query], options, ar_options)
    
    respond_to do |format|
      format.html { render :index }
      format.xml  { render :xml=>@pages }
    end
  end
  
protected
  def find_book
    @book = Book.find(params[:book_id].to_i)
  end
  
  def save_sound_file(page, file_data)
    File.open(page.sound_file_name, "wb") {|f| f.write file_data.read}
  end

  def not_allowed
    render :text => 'not allowed', :status => 404
  end
end
